/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dto.AccountDTO;
import east2west.dto.FunctionDTO;
import east2west.dto.RoleDTO;
import east2west.dto.RoleFunctionDTO;
import east2west.service.AdminServicer;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author anhtuan
 */
public class AdminUserAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String ERR = "err";
    private final static String TRUE = "true";
    private final static String ROLEEDIT = "roleedit";


    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element :
     * ( <action parameter="method" .../> )
     */


    public ActionForward listUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminServicer serAdmin = new AdminServicer();
        ArrayList<AccountDTO> listAccount = serAdmin.readByAllAccount();
        ArrayList<RoleDTO> listRole = serAdmin.readByAllRole();
        request.setAttribute("listRole", listRole);
        request.setAttribute("listAccount", listAccount);
        request.setAttribute("page", "listUser.page");
        return mapping.findForward(SUCCESS);
    }

   
    public ActionForward Insert(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AccountDTO accDTO = new AccountDTO();
        AdminServicer serAdmin = new AdminServicer();
        accDTO.setUserName(request.getParameter("fr1use"));
        accDTO.setPassWord(request.getParameter("fr1pass"));
        accDTO.setEmail(request.getParameter("fr1mail"));
        accDTO.setRoleId(Integer.parseInt(request.getParameter("role")));
        accDTO.setFullName(request.getParameter("fr1name"));
        accDTO.setPhoneNumber(request.getParameter("fr1phone"));
        accDTO.setAddress(request.getParameter("fr1add"));
        if (serAdmin.createAccount(accDTO)) {
            return mapping.findForward(TRUE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward Update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AccountDTO accDTO = new AccountDTO();
        AdminServicer serAdmin = new AdminServicer();
        accDTO.setUserName(request.getParameter("fr1useedit"));
        accDTO.setPassWord(request.getParameter("fr1pass"));
        accDTO.setEmail(request.getParameter("fr1mail"));
        accDTO.setRoleId(Integer.parseInt(request.getParameter("role")));
        accDTO.setFullName(request.getParameter("fr1name"));
        accDTO.setPhoneNumber(request.getParameter("fr1phone"));
        accDTO.setAddress(request.getParameter("fr1add"));
        if (serAdmin.updateAccount(accDTO)) {
            return mapping.findForward(TRUE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward Delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminServicer serAdmin = new AdminServicer();
        String username = request.getParameter("fr1useedit");
        if (serAdmin.deleteAccount(username)) {
            return mapping.findForward(TRUE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward listRole(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AdminServicer serAdmin = new AdminServicer();
        ArrayList<RoleDTO> listRole = serAdmin.readByAllRole();
        ArrayList<FunctionDTO> listFunction = serAdmin.readByAllFunction();
        request.setAttribute("listRole", listRole);
        request.setAttribute("listFunc", listFunction);
        request.setAttribute("page", "listRole.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward roleEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        AdminServicer serAdmin = new AdminServicer();
        RoleDTO roleDTO = serAdmin.readRoleById(roleId);
        ArrayList<FunctionDTO> listFunction = serAdmin.readByAllFunction();
        ArrayList<RoleFunctionDTO> listRoleFunction = serAdmin.readRoleFunctionByRoleId(roleId);
        request.setAttribute("roleDTO", roleDTO);
        request.setAttribute("listRoleFunction", listRoleFunction);
        request.setAttribute("listFunction", listFunction);
        //request.setAttribute("page", "editRole.page");
        return mapping.findForward(ROLEEDIT);
    }
}
